<?php
switch($do){
	case "add":
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if($is_submit){
			$return_data = array('error'=>true,"message"=>'操作失败');
			//接收通知入库并发送
			$data = array();
			$data['msg_type'] = isset($_REQUEST['msg_type']) ? $_REQUEST['msg_type']:'';
			$data['msg_from'] = $_G['user_id'];
			$data['msg_content'] = isset($_REQUEST['msg_content']) ? $_REQUEST['msg_content']:'';
			$data['msg_opt'] = isset($_REQUEST['msg_opt']) ? implode(',',$_REQUEST['msg_opt']):'';
			$data['create_time'] = TIMESTAMP;
			$insert_id = DB::insert('msg',$data,true);
			if($insert_id){
				$return_data = array('error'=>false,"message"=>'操作成功');
			}
			//DEBUG 接口发送消息
			$msg_from_realname = ext::user_realname($_G['user_id']);
			//DEBUG 发送班级组消息
			$msg_url = $_G['config']['msg'][1]['host'].'/msg/broadcast';
			$msg_post = array(
				'from'=> $msg_from_realname,// 消息发送者姓名
				'to'=> '',// 接收消息的群组id
				'data'=> $data['msg_content'],// 消息内容
			);
			foreach($_REQUEST['msg_opt'] AS $key => $value){
				if($value){
					$msg_post['to'] = $value;
					//DEBUG 框架内置发送函数
					//dfsockopen($msg_url, '100', $msg_post);
					//DEBUG 独立CURL模拟POST
					$srv_ip = str_replace(array('http://',':7000/msg/broadcast'),'',$msg_url);//你的目标服务地址或频道.
					$srv_port = 7000;
					$url = '/msg/broadcast'; //接收你post的URL具体地址 
					$fp = '';
					$resp_str = '';
					$errno = 0;
					$errstr = '';
					$timeout = 10;
					$post_str = "from=".$msg_post['from']."&data=".$msg_post['data']."&to=".$msg_post['to'];//要提交的内容.
					//echo $url_str;
					if ($srv_ip == ''){
						$return_data = array('error'=>true,"message"=>'消息服务器设置为空');
					}
					//echo($srv_ip);
					$fp = fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout);
					if (!$fp){
						$return_data = array('error'=>true,"message"=>'消息服务器不响应');
					}else{
						$content_length = strlen($post_str);
						$post_header = "POST $url HTTP/1.1\r\n";
						$post_header .= "Content-Type: application/x-www-form-urlencoded\r\n";
						$post_header .= "User-Agent: MSIE\r\n";
						$post_header .= "Host: ".$srv_ip."\r\n";
						$post_header .= "Content-Length: ".$content_length."\r\n";
						$post_header .= "Connection: close\r\n\r\n";
						$post_header .= $post_str."\r\n\r\n";
						fwrite($fp,$post_header);
							while(!feof($fp)){
							$resp_str .= fgets($fp,512);//返回值放入$resp_str
						}
						fclose($fp);
					}
				}
			}
			echo json_ext($return_data);
			die();
		}else{
			//DEBUG 取出用户所在的所有班级
			$class_array = array();
			$action_class_path = libfile('hschool/class', 'class/admin','..');
			if(file_exists($action_class_path)){
				require_once $action_class_path;
				$hschool_class = new hschool_class();
				$class_array_1 = $hschool_class->get_master_class();
				$class_array_2 = $hschool_class->get_created_class();
				$class_array_all = array_merge($class_array_1,$class_array_2);
				foreach($class_array_all AS $key => $value){
					$class_array[$value['class_id']] = $value;
				}
			}
			include template('admin/hschool/msg_add');
		}
		break;
	case "show":
		//DEBUG 获取消息信息
		$msg_info_array = array();
		$msg_id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
		if($msg_id){
			$sql = "SELECT m.* from ".DB::table('msg')." AS m WHERE msg_id = '".$msg_id."' LIMIT 1";
			$msg_info_array = DB::fetch_first($sql);
		}
		include template('admin/hschool/msg_show');
		break;
	case "delete":
		//DEBUG 删除消息信息
		$msg_id = isset($_REQUEST['id']) ? $_REQUEST['id']:array();
		if($msg_id){
			$update = array(
				'isdelete'=> 1
			);
			$where = ' msg_id = '.$msg_id.'';
			$effects = DB::update('msg',$update,$where);
		}
		//DEBUG 接收用户信息 end
		$return_array = array("success"=>1,"message"=>"操作成功","data"=>array());
		echo json_encode($return_array);
		break;
	case "index":
		//DEBUG 所有前端角色 学生 老师 家长
        $page_array = array();
        $sqlcount = $sql = $wheresql = $title_keyword = $pageNum = $numPerPage = '';
        $pageNum = isset($_REQUEST['pageNum']) ? $_REQUEST['pageNum'] : 1;
        $numPerPage = isset($_REQUEST['numPerPage']) ? $_REQUEST['numPerPage'] : 10;
        $pagestart = ($pageNum - 1) * $numPerPage;
        $sqlcount = "SELECT count(*) from ".DB::table('msg')." AS m WHERE m.isdelete=0 ".$wheresql;
        $pagetotal = DB::result_first($sqlcount);
        $sql = "SELECT m.* from ".DB::table('msg')." AS m WHERE m.isdelete=0 ORDER BY m.msg_id DESC LIMIT ".$pagestart.",".$numPerPage;
        $page_array = DB::fetch_all($sql);
		foreach($page_array AS $key => $value){
			$class_id_string = str_replace('C', '', strtoupper($value['msg_opt']));
			if(strpos($class_id_string,',')){
				$class_id_array = explode(',',$class_id_string);
				$class_name_array = ext::class_name($class_id_array);
				$class_name_string = '';
				foreach($class_name_array AS $k => $v){
					$class_name_string .= '&nbsp;&nbsp;'.$v['class_name'];
				}
			}else{
				$class_name_string = ext::class_name($class_id_string);
			}
			$value['class_name_string'] = $class_name_string;
			$page_array[$key] = $value;
		}
		//总页数
		$realpages = @ceil($pagetotal / $numPerPage);
		if($realpages <= 0){
			$realpages=1;
		}
		//前一页
		$page_pre = max(1, intval(($pageNum-1)));
		//后一页
		$page_next = min(intval(($pageNum+1)), $realpages);
		include template('admin/hschool/msg');
		break;
}
?>